﻿using System;
using SWS = System.Web.Security;

namespace Ventigrate.Shared.ExternalMembership.Internal
{
    public static class Roles
    {
        #region Create

        public static void CreateRole(string roleName)
        {
            using (var ctx = ExtranetContext.Default)
            {
                ctx.RoleProvider.CreateRole(roleName);
            }
        }

        public static void AddUserToRole(string username, string roleName)
        {
            using (var ctx = ExtranetContext.Default)
            {
                ctx.RoleProvider.AddUsersToRoles(new string[] { username }, new string[] { roleName });
            }
        }

        #endregion

        #region Delete

        public static void DeleteRole(string roleName)
        {
            using (var ctx = ExtranetContext.Default)
            {
                ctx.RoleProvider.DeleteRole(roleName, true);
            }
        }

        public static void RemoveUserFromRole(string username, string roleName)
        {
            using (var ctx = ExtranetContext.Default)
            {
                Roles.RemoveUsersFromRole(new string[] { username }, roleName);
            }
        }

        public static void RemoveUsersFromRole(string[] usersInRole, string roleName)
        {
            using (var ctx = ExtranetContext.Default)
            {
                ctx.RoleProvider.RemoveUsersFromRoles(usersInRole, new string[] { roleName });
            }
        }

        #endregion

        #region Get

        public static bool RoleExists(string roleName)
        {
            bool result = false;

            using (var ctx = ExtranetContext.Default)
            {
                result = ctx.RoleProvider.RoleExists(roleName);
            }

            return result;
        }

        public static string[] GetAllRoles()
        {
            string[] result = null;

            using (var ctx = ExtranetContext.Default)
            {
                result = ctx.RoleProvider.GetAllRoles();
            }

            return result;
        }

        public static string[] GetUsersInRole(string roleName)
        {
            string[] result = null;

            using (var ctx = ExtranetContext.Default)
            {
                result = ctx.RoleProvider.GetUsersInRole(roleName);
            }

            return result;
        }

        public static bool IsUserInRole(string username, string roleName)
        {
            bool result = false;

            using (var ctx = ExtranetContext.Default)
            {
                result = ctx.RoleProvider.IsUserInRole(username, roleName);
            }

            return result;
        }

        #endregion
    }
}
